package me.mononin.dilechat.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import me.mononin.common.common.app.Activity;
import me.mononin.common.common.widget.PortraitView;
import me.mononin.dilechat.R;
import me.mononin.dilechat.fragments.main.ContactFragment;
import me.mononin.dilechat.fragments.main.GroupFragment;
import me.mononin.dilechat.fragments.main.ChatFragment;
import me.mononin.dilechat.helper.NavHelper;
import me.mononin.factory.persistence.Account;

public class MainActivity extends Activity
        implements BottomNavigationView.OnNavigationItemSelectedListener,
        NavHelper.OnTabChangeListener<Integer>,View.OnClickListener{


    @BindView(R.id.action_title)
    TextView actionTitle;

    @BindView(R.id.action_menu_bar)
    BottomNavigationView menuBar;

    @BindView(R.id.main_drawer_layout)
    DrawerLayout mDrawer;

    @BindView(R.id.main_nav_view)
    NavigationView mNavView;

    @BindView(R.id.main_ic_user)
    PortraitView mPortrait;

    private NavHelper<Integer> mNavHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        menuBar.setOnNavigationItemSelectedListener(this);
        mNavHelper = new NavHelper<>(this, R.id.action_container,
                getSupportFragmentManager(), this);

        mNavHelper.add(R.id.action_chat, new NavHelper.Tab<>(ChatFragment.class, R.string.action_chat))
                .add(R.id.action_group, new NavHelper.Tab<>(GroupFragment.class, R.string.action_group))
                .add(R.id.action_message, new NavHelper.Tab<>(ContactFragment.class, R.string.action_message));

        View navHeader = mNavView.inflateHeaderView(R.layout.nav_header);
        CircleImageView navHeadIc = navHeader.findViewById(R.id.nav_user_ic);
        navHeadIc.setOnClickListener(this);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        if(Account.isComplete()) {
            // 判断用户信息是否完全，完全则走正常流程
            return super.initArgs(bundle);
        }else{
            AccountActivity.start(this);
            return false;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        Menu menu = menuBar.getMenu();

        //该函数执行给定菜单标识符所对应的操作
        menu.performIdentifierAction(R.id.action_chat, 0);

        //初始化头像
        mPortrait.setup(Glide.with(this),Account.getUser());
    }

    /**
     * 对外打开MainActivity的方法
     * @param context 上下文
     */
    public static void show(Context context){
        context.startActivity(new Intent(context,MainActivity.class));
    }

    /**
     * bottomNavigationView Item选中触发
     * @param item item
     * @return 返回选中的itemId
     */
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        //调用helper
        return mNavHelper.returnClickMenu(item.getItemId());
    }


    @Override
    public void onTabChange(NavHelper.Tab<Integer> oldTab, NavHelper.Tab<Integer> newTab) {
        actionTitle.setText(newTab.extra);
    }

    /**
     * 注解点击事件触发
     * @param v view
     */
    @OnClick({R.id.main_ic_user,R.id.main_ic_search,R.id.main_ic_add})
    public void clickEvent(View v){
        switch (v.getId()){
            case R.id.main_ic_user:
                mDrawer.openDrawer(GravityCompat.START);
                break;
            case R.id.main_ic_search:
                // 在群的界面的时候，点击顶部的搜索就进入群搜索界面
                // 其他都为人搜索的界面
                int type = Objects.equals(mNavHelper.getCurrentTab().extra, R.string.action_group) ?
                        SearchActivity.TYPE_GROUP : SearchActivity.TYPE_USER;
                SearchActivity.show(this, type);
                break;
            case R.id.main_ic_add:
                GroupCreateActivity.show(this);
                break;
            default:
                break;
        }
    }

    /**
     * 原生点击事件触发
     * @param v view
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.nav_user_ic:
                PersonalActivity.show(this,Account.getUserId());
                break;
            default:
                break;
        }
    }
}
